import vue from 'vue'
import axios from 'axios'
import {getCookie} from '../until/until'

// axios.default.baseURL = "http://localhost:8088"
axios.create({
     // baseURL: 'http://localhost:8088',
	 baseURL: process.env.BASE_API,
     timeout: 5000,
//      headers: {
//          'Content-Type': 'application/json',
//          'Cache-Control': 'no-cache',
//          'authorization': window.sessionStorage.getItem('token')
//      }
});

vue.prototype.$ajax = axios
// vue.prototype.HOST = "http://localhost:8088"

//设置request 拦截器,
axios.interceptors.request.use(config => {
	const token = localStorage.getItem('token')
	if( token ){
		config.headers.Authorization = `token ${token}`;
	}
	return config
	// const token = getCookie('session')//获取cookie
	// // config.data = JSON.stringify(config.data)
	// console.log(config.data)
	// config.headers = {
	// 	'Content-Type': 'application/x-www-form-urlencoded',//设置跨域头部
	// }
	// if(token){
	// 	config.params = {'token':token}
	// }
	// return config
},err => {
	return Promise.reject(err)
})

// http response拦截器
axios.interceptors.response.use(
	response =>{
	return response
},error => {
	// return Promise.reject(error.response.data)
	if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 返回 401 清除token信息并跳转到登录页面
					localStorage.removeItem('token')
                    router.replace({
                        path: 'login',
                        query: {redirect: router.currentRoute.fullPath}
                    })
            }
        }
        return Promise.reject(error.response.data)   // 返回接口返回的错误信息
})

export default axios

 